import CryptoJS from 'crypto-js'

export default {
    encrypt(word, key = '1234567890ABCDEF', n = 16) {
        key = this.check(key, n)
        let k = CryptoJS.enc.Utf8.parse(key);
        let srcs = CryptoJS.enc.Utf8.parse(word);
        let enc = CryptoJS.AES.encrypt(srcs, k, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        return enc.toString()
    },
    decrypt(word, key = '1234567890ABCDEF', n = 16) {
        key = this.check(key, n)
        let k = CryptoJS.enc.Utf8.parse(key)
        let dec = CryptoJS.AES.decrypt(word, k, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        return CryptoJS.enc.Utf8.stringify(dec).toString()
    },
    check(key = '1234567890ABCDEF', n = 16) {
        if (key.length < n) {
            while (key.length < n) {
                key += key
            }
        }
        return key.substring(0, n)
    }
}